﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
using System.Web.Routing;
namespace ADShop.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (Session["Admin"] == null)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Oops",
                    action = "Index"
                }));
            }
        }
        private EShopEntities1 db = new EShopEntities1(); // ADShopEntities db = new ADShopEntities();

        //
        // GET: /Admin/Product/

        public ActionResult Index()
        {
            var products = db.Product.Include(p => p.Brand).Include(p => p.Category).Include(p => p.PromotionsProduct).Include(p => p.StatusProduct);
            return View(products.ToList());
        }

        
        /// <summary>
        /// Lấy ảnh cho từng sản phẩm
        /// </summary>
        /// <param name="id">Product ID của sản phẩm</param>
        /// <returns>Trả về một đường dẫn ảnh</returns>
        public string GetImages(int id)
        {
            if (!db.ImagesProduct.Any(d => d.IDProduct == id))
            {
                return string.Format("<img src='http://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg' width='75' height='75' alt='No Images Aviable !' />");
            }
            return string.Format("<img src='{0}' width='75' height='75' alt='{1}' />", db.ImagesProduct.Where(d => d.IDProduct == id).FirstOrDefault().LinkImagesProduct, db.ImagesProduct.Where(d => d.IDProduct == id).FirstOrDefault().DescriptionImages);
        }

        //
        // GET: /Admin/Product/Details/5
        
        public ActionResult Details(int id = 0)
        {
            Product product = db.Product.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /Admin/Product/Create

        public ActionResult Create()
        {
            ViewBag.IDBrand = new SelectList(db.Brand, "IDBrand", "BrandName",0);
            ViewBag.IDCategory = new SelectList(db.Category, "IDCategory", "NameCategory",0);
            ViewBag.IDPromotions = new SelectList(db.PromotionsProduct, "IDPromotions", "IDPromotions",0);
            ViewBag.IDStatus = new SelectList(db.StatusProduct, "IDStatus", "NameStatus",0);
            return View();
        }

        //
        // POST: /Admin/Product/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Product.Add(product);
                db.SaveChanges();
                ImagesProduct ip = new ImagesProduct();
                ip.IDProduct = db.Product.Max(d => d.IDProduct);
                ip.LinkImagesProduct = product.ProductImage;
                ip.DescriptionImages = product.NameProduct;
                db.ImagesProduct.Add(ip);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IDBrand = new SelectList(db.Brand, "IDBrand", "BrandName", product.IDBrand);
            ViewBag.IDCategory = new SelectList(db.Category, "IDCategory", "NameCategory", product.IDCategory);
            ViewBag.IDPromotions = new SelectList(db.PromotionsProduct, "IDPromotions", "IDPromotions", product.IDPromotions);
            ViewBag.IDStatus = new SelectList(db.StatusProduct, "IDStatus", "NameStatus", product.IDStatus);
            return View(product);
        }

        //
        // GET: /Admin/Product/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Product product = db.Product.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            ViewBag.IDBrand = new SelectList(db.Brand, "IDBrand", "BrandName", product.IDBrand);
            ViewBag.IDCategory = new SelectList(db.Category, "IDCategory", "NameCategory", product.IDCategory);
            ViewBag.IDPromotions = new SelectList(db.PromotionsProduct, "IDPromotions", "IDPromotions", product.IDPromotions);
            ViewBag.IDStatus = new SelectList(db.StatusProduct, "IDStatus", "NameStatus", product.IDStatus);
            return View(product);
        }

        //
        // POST: /Admin/Product/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDBrand = new SelectList(db.Brand, "IDBrand", "BrandName", product.IDBrand);
            ViewBag.IDCategory = new SelectList(db.Category, "IDCategory", "NameCategory", product.IDCategory);
            ViewBag.IDPromotions = new SelectList(db.PromotionsProduct, "IDPromotions", "IDPromotions", product.IDPromotions);
            ViewBag.IDStatus = new SelectList(db.StatusProduct, "IDStatus", "NameStatus", product.IDStatus);
            return View(product);
        }

        //
        // GET: /Admin/Product/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Product product = db.Product.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // POST: /Admin/Product/Delete/5

        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Product.Find(id);
            db.Product.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}